package com.ncmem.up6;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;

import com.ncmem.up6.storage.*;
import net.sf.json.JSONObject;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * Created by jmzy on 2021/1/5.
 */
public class ConfigReader {
    public JSONObject m_files;
    public PathTool m_pt;
    public ReadContext m_jp;

    public ConfigReader(){
        this.m_pt = new PathTool();
        ClassPathResource res = new ClassPathResource("config/config.json");
        try {
            InputStream inputStream = res.getInputStream();
            String json = PathTool.getFileContent(inputStream);
            this.m_files = JSONObject.fromObject(json);
            this.m_jp = JsonPath.parse(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据文件路径读取文件内容
     *
     * @param fileInPath
     * @throws IOException
     */
    public void getFileContent(Object fileInPath) throws IOException {
        BufferedReader br = null;
        if (fileInPath == null) {
            return;
        }
        if (fileInPath instanceof String) {
            br = new BufferedReader(new FileReader(new File((String) fileInPath)));
        } else if (fileInPath instanceof InputStream) {
            br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
        }
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }

    /**
     * 将配置加载成一个json对象
     * @param name
     * @return
     */
    public JSONObject module(String name)
    {
        String path = this.m_jp.read(name);
        ClassPathResource res = new ClassPathResource(path);
        String data = null;
        try {
            InputStream inputStream = res.getInputStream();
            data = PathTool.getFileContent(inputStream);
            //data = FileTool.readAll(res.getFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.fromObject(data);
    }

    /**
     * 将JSON值转换成一个路径
     * @param name
     * @return
     */
    public String readPath(String name)
    {
        Object o = this.m_jp.read(name);
        String v = o.toString();
        v = this.m_pt.MapPath(v);
        return v;
    }

    public JSONObject read(String name)
    {
        Object o = this.m_jp.read(name);
        return JSONObject.fromObject(o);
    }

    /**
     * 以jsonpath方式读取config.json中的string值
     * @param name
     * @return
     */
    public String readString(String name)
    {
        Object o = this.m_jp.read(name);
        return o.toString();
    }

    /**
     * 以jsonpath方式读取config.json中的bool值
     * @param name
     * @return
     */
    public boolean readBool(String name)
    {
        Object o = this.m_jp.read(name);
        return Boolean.parseBoolean(o.toString());
    }

    /**
     * 获取存储类型
     * @return
     */
    public static StorageType storageType(){
        ConfigReader cr = new ConfigReader();
        String sto = cr.readString("Storage.type");
        if(sto.compareToIgnoreCase("FastDFS") ==0) return StorageType.FastDFS;
        if(sto.compareToIgnoreCase("Minio") ==0) return StorageType.Minio;
        if(sto.compareToIgnoreCase("OBS") ==0) return StorageType.OBS;
        return StorageType.IO;
    }

    /**
     * 传输加密
     * @return
     */
    public static boolean postEncrypt() {
        ConfigReader cr = new ConfigReader();
        JSONObject sec = cr.module("path");
        JSONObject security = sec.getJSONObject("security");
        boolean encrypt = security.getBoolean("encrypt");
        return encrypt;
    }

    /**
     * 存储加密
     * @return
     */
    public static boolean storageEncrypt(){
        ConfigReader cr = new ConfigReader();
        return cr.readBool("Storage.encrypt");
    }

    /**
     * 块数据写入器
     * @return
     */
    public static FileBlockWriter blockWriter(){
        FileBlockWriter w = null;
        StorageType st = ConfigReader.storageType();
        if(st==StorageType.FastDFS) w = new DfsBlockWriter();
        else if(st==StorageType.Minio) w = new MinioBlockWriter();
        else if(st==StorageType.OBS) w = new ObsWriter();
        else  w = new FileBlockWriter();
        return w;
    }

    /**
     * 块数据读取器
     * @return
     */
    public static FileBlockReader blockReader(){
        FileBlockReader w = null;
        StorageType st = ConfigReader.storageType();
        if(st==StorageType.FastDFS) w = new DfsBlockReader();
        else if(st==StorageType.Minio) w = new MinioBlockReader();
        else if(st==StorageType.OBS) w = new ObsReader();
        else  w = new FileBlockReader();
        return w;
    }
}
